<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\common\model\log;

use think\Model;

/**
 * Description of arvatoPushLogs
 *
 * @author dxx
 */
class arvatoApiPushLogs extends Model {

    protected $table = 'arvato_Api_Push_Logs';
    private $_baseTable = 'arvato_Api_Push_Logs';
    protected $connection = ['database' => 'jooj_log'];

    public function __construct($data = []) {
        $date = date('Ymd', time());
        $this->table = $this->_baseTable . '_' . $date;
        parent::__construct($data);
    }

    private function _createTable() {
        $sql = 'CREATE TABLE ' . $this->table . ' LIKE ' . $this->_baseTable;
        return $this->execute($sql);
    }

    public function addLog($inPut, $outPut, $url, $original_result = '',$tryTimes = 0) {
        $isSuccess = (isset($outPut['StatusCode']) && ($outPut['StatusCode'] == 'S01')) ? 1 : 0;

        $data = [
            'url'             => $url,
            'param'           => $inPut,
            'result'          => json_encode($outPut),
            'api_user_id'     => 0,
            'log_time'        => time(),
            'is_success'      => $isSuccess,
            'original_result' => json_encode($original_result),
            'try_times' => $tryTimes,
        ];

        try {
            $this->create($data);
        } catch (\Exception $e) {
            if (strstr($e->getMessage(), 'Base table or view not found')) {
                if (FALSE !== $this->_createTable()) {
                    $this->create($data);
                } else {
                    
                }
            }
        }
    }

//    public function __destruct() {
//        self::$links[$this->class]=null;
//    }
}
